<template>
  <vxe-grid ref="table" v-bind="gridOptions" v-on="gridEvents">
    <template #option="{ row }">
      <template class="table-option">
        <Tooltip content="系统生成数据不允许编辑" :disabled="row.source != DataSourceType.SystemGeneration">
          <vxe-button :disabled="row.source === DataSourceType.SystemGeneration" status="primary" content="编辑"
                      @click="editRowEvent(row)"/>
        </Tooltip>
        <Tooltip content="系统生成数据不允许删除" :disabled="row.source != DataSourceType.SystemGeneration">
          <vxe-button :disabled="row.source === DataSourceType.SystemGeneration" status="danger" content="删除"
                      @click="deleteRowEvent(row)"/>
        </Tooltip>
      </template>
    </template>
  </vxe-grid>
  <ChildDictDataEdit ref="edit" @ok="saveSuccess"/>
</template>

<script setup>
import {DataSourceType} from "@/model/common/SysEnums";
</script>

<script>
import {getPageChildDictData, deleteChildDictDataById} from "@/api/sys/childDictData";
import {getDictMap} from "@/util";
import {getChildDictById} from "@/api/sys/childDict";
import {getAllDictData} from "@/api/sys/dictData";
import ChildDictDataEdit from "./SysChildDictDataEdit.vue";
import {Message} from "view-ui-plus";
import {RESPONSE_CODE_SUCCESS} from "@/constant/response";

export default {
  name: "SysChildDictDataMgr",
  expose: ['reloadTableData'],
  props: ['childDictId'],
  components: {ChildDictDataEdit},
  data() {
    return {
      dictCode: undefined,
      currDict: undefined,
      currChildDict: undefined,
      enableDisableDict: undefined,
      dataSourceTypeDict: undefined,
      dictDatas: undefined,
      gridOptions: {
        toolbarConfig: {
          buttons: [
            {code: 'add', name: '新增'}
          ],
        },
        expandConfig: {
          showIcon: false
        },
        pagerConfig: {
          pageSize: 10
        },
        columns: [
          {type: 'seq', width: 60, title: '序号'},
          {field: 'dictDataCode', title: '编码'},
          {field: 'name', title: '名称'},
          {field: 'orderValue', title: '排序值'},
          {field: 'status', title: '状态', formatter: this.formatterEnableDisableDict},
          {field: 'source', title: '数据来源', formatter: this.formatterDataSourceTypeDict},
          {field: 'dictCode', title: '字典编码'},
          {field: 'dictName', title: '字典名称'},
          {field: 'childDictCode', title: '子字典编码'},
          {field: 'childDictName', title: '子字典名称'},
          {field: 'remark', title: '备注'},
          {title: '操作', width: 150, slots: {default: 'option'}}
        ],
        formConfig: {
          items: [
            {field: 'likeName', title: '名称：', span: 6, itemRender: {name: 'input'}},
            {field: 'likeCode', title: '编码：', span: 6, itemRender: {name: 'input'}},
            {itemRender: {name: '$button', props: {content: '查询', type: 'submit', status: 'primary'}}},
            {itemRender: {name: '$button', props: {content: '重置', type: 'reset'}}}
          ]
        },
        proxyConfig: {
          ajax: {
            query: ({page, form}) => {
              return new Promise(resolve => {
                getPageChildDictData({
                  pageIndex: page.currentPage,
                  pageSize: page.pageSize,
                  childDictId: this.childDictId,
                  ...form
                }).then(res => {
                  let {data} = res
                  for (const item of data) {
                    item.dictCode = this.currDict.code
                    item.dictName = this.currDict.name
                    item.childDictCode = this.currChildDict.code
                    item.childDictName = this.currChildDict.name
                    for (const dictData of this.dictDatas) {
                      if (item.dictDataId == dictData.id) {
                        item.dictDataCode = dictData.code
                        break
                      }
                    }
                  }
                  resolve(res)
                })
              })
            }
          }
        }
      },
      gridEvents: {
        toolbarButtonClick: this.toolbarButtonClick,
      },
    }
  },
  created() {
    let query = this.$route.query;
    this.dictCode = query.dictCode
    this.reloadTableData()
  },
  methods: {
    reloadTableData() {
      Promise.all([
        getDictMap("YesNo:EnableDisable", 'DataSourceType', this.dictCode),
        getChildDictById(this.childDictId),
        getAllDictData({dictCode: this.dictCode})
      ]).then(res => {
        let dicts = res[0]
        this.currChildDict = res[1].data
        this.dictDatas = res[2].data
        if (!this.dictDatas) {
          this.dictDatas = [];
        }
        this.enableDisableDict = dicts.get("YesNo:EnableDisable")
        this.dataSourceTypeDict = dicts.get("DataSourceType")
        this.currDict = dicts.get(this.dictCode)
        this.$refs.table.commitProxy('reload')
      })
    },
    editRowEvent(row) {
      let data = this.$utils.clone(row)
      this.$refs.edit.show({
        title: "子字典数据",
        data: data
      })
    },
    deleteRowEvent(row) {
      this.$Modal.confirm({
        title: '删除子字典数据',
        content: '确认删除子字典数据?',
        okText: '删除',
        onOk: () => {
          deleteChildDictDataById({
            id: row.id
          }).then(res => {
            if (res.code == RESPONSE_CODE_SUCCESS) {
              this.$emit('reloadData')
              Message.success("删除成功")
            }
          })
        }
      })
    },
    toolbarButtonClick({code}) {
      switch (code) {
        case 'add': {
          this.$refs.edit.show({
            title: "添加子字典数据",
            data: {
              dictCode: this.dictCode,
              childDictCode: this.currChildDict.code
            }
          })
          return;
        }
        default: {
          return
        }
      }
    },
    saveSuccess() {
      this.$refs.table.commitProxy('reload')
      this.$emit('reloadData')
    },
    formatterEnableDisableDict({cellValue}) {
      if (this.enableDisableDict) {
        return this.enableDisableDict.dataMap.get(cellValue).name
      }
      return cellValue
    },
    formatterDataSourceTypeDict({cellValue}) {
      if (this.dataSourceTypeDict) {
        return this.dataSourceTypeDict.dataMap.get(cellValue).name
      }
      return cellValue
    },
  }
}
</script>

<style scoped>

</style>
